package com.supermap.business.disaster;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dubbo.api.cloud.key.KeyWordService;
import com.supermap.dubbo.api.disaster.DisasterBase;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.disaster.DisasterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:langhongshuai
 * @Description:灾情接口层
 * @Date:18/1/29 下午2:37
 */
@Controller
@RequestMapping("DisasterController")
public class DisasterController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(DisasterController.class);

    @Reference(timeout = 100000)
    private DisasterService disasterService;
    @Reference(timeout = 100000)
    private KeyWordService keyWordService;
    /**
     * 通过用户id和区域code获得用户的灾情信息
     */
    @RequestMapping("/getDisasterByUIdArea")
    @ResponseBody
    public JSONUtil getDisasterByUIdArea(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String areaCode = request.getParameter("areaCode");
        return disasterService.getDisasterByUIdArea(userId, areaCode);
    }

    /**
     * 获取灾害等级
     * */
    @RequestMapping("/getDisasterLev")
    @ResponseBody
    public List<Map<String,Object>> getDisasterLev(){
        return  disasterService.getDisasterLev();
    }
    /**
     * 添加灾情
     *
     * @return
     */
    @PostMapping(value = "/addDisaster")
    @ResponseBody
    public JSONUtil addDisaster(@RequestBody DisasterBase disaster) {
        JSONUtil jsonUtil=disasterService.addDisaster(disaster);
        //违规字过滤
        Map<String,Object> map=keyWordService.getKeyWord(disaster.getDescript());
        String typeCode="zq";
        String typeTitle="灾情";
        Integer typeId=disasterService.getAddDisasterId();
        String dateTime1=  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        if(map!=null&&map.size()>0){
            keyWordService.upKeyWord(typeCode,typeId,1);
            String keygroup= (String)map.get("keygroup");
            String keyword= (String)map.get("keyword");
            keyWordService.putKeyWord(Integer.valueOf(disaster.getUserId()),typeCode,typeId,typeTitle,disaster.getDescript(),keyword,keygroup,dateTime1);
            return  jsonUtil.setMsg("操作成功,但该内容违规,待管理员审核即可有效").setCode(208);
        }
        return  jsonUtil;
    }

    /**
     * 获得所有的灾情信息
     *
     * @return
     */
    @RequestMapping("/getAllDisaster")
    @ResponseBody
    public JSONUtil getAllDisaster() {
        return disasterService.getAllDisaster();
    }

    /**
     * 删除灾情
     * @param request
     * @return
     */
    @RequestMapping("/delDisaster")
    @ResponseBody
    public JSONUtil delDisaster(HttpServletRequest request){
        String disasterId = request.getParameter("disasterId");
        return disasterService.delDisaster(Integer.parseInt(disasterId));
    }

    /**
     * 根据区域编码得到灾情
     * @param request
     * @return
     */
    @RequestMapping("/getDisasterByareacode")
    @ResponseBody
    public JSONUtil getDisasterByareacode(HttpServletRequest request){
        String areacode = request.getParameter("areacode");
        return disasterService.getDisasterByareacode(areacode);
    }

    /**
     * 获得灾情
     */
    @RequestMapping("/getDisasters")
    @ResponseBody
    public JSONUtil getDisasters(HttpServletRequest request){
        String type = request.getParameter("cropid");
        String areaCode = request.getParameter("areaCode");
        String disasterType = request.getParameter("disasterType");
        String dateTime = request.getParameter("dateTime");
        return disasterService.getDisasters(type,areaCode,disasterType,dateTime);
    }
}
